global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"





*0) BOOTSTRAP SAMPLE
/*

set seed 20170526
cap mkdir "$SSDIMed/data/analysis/bootstrap"
local bsnum 500
use fipscounty_firstnm  using "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear 
rename (fipscounty_firstnm ) (fipscounty_init)
gduplicates drop _all, force 
save "$SSDIMed/data/analysis/bootstrap/allcountiestemp.dta", replace 
forval n=1/`bsnum'{
use $SSDIMed/data/analysis/bootstrap/allcountiestemp.dta, clear 
bsample 
rename * *`n'
gen clustervar`n'=_n
gen counter=_n 
save "$SSDIMed/data/analysis/bootstrap/sample_cluster_county`n'.dta", replace
}

use "$SSDIMed/data/analysis/bootstrap/sample_cluster_county1.dta", clear 
forval n=2/`bsnum'{
qui merge 1:1 counter using "$SSDIMed/data/analysis/bootstrap/sample_cluster_county`n'", nogen 
}
save "$SSDIMed/data/analysis/bootstrap/samples_cluster_county.dta", replace 
*/


*1) BOOTSTRAP FOR ENTRY
/*
local sample 5152
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
***verify same as current results
local controls_02 i.fipscounty_firstnm_g
local y incidence_pop_age_atapp
local w pop_age_atapp
local clust county_mofd
local ctrl "02"
timer on 1 
reghdfe `y' age52 UR age52xUR [aw = `w'], abs(`controls_`ctrl'') cluster(`clust')
timer off 1 
timer on 2
reghdfe `y' age52 UR age52xUR [aw = `w'], abs(`controls_`ctrl'') 
timer off 2
timer list 

cd "$SSDIMed/results/estimates/x-age52xUR/"
cap postclose entry_unempatappx52
postfile entry_unempatappx52 intercept UR age52  age52xUR sample str30 ctrl using "bootstrap_incidence_cluster_county.dta", replace 
local bsnum 500
forval n=1/`bsnum'{
di "`n'"
qui{
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
rename (fipscounty_firstnm  ) (fipscounty_init`n' )
joinby fipscounty_init`n'  using "$SSDIMed/data/analysis/bootstrap/samples_cluster_county.dta"
rename (fipscounty_init`n' ) (fipscounty_init) 
local y incidence_pop_age_atapp
local w pop_age_atapp
local clust county_mofd
reghdfe `y' age52 UR age52xUR [w=`w'], abs(fipscounty_init)  
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post entry_unempatappx52 (`intercept') (`UR') (`age52') (`age52xUR') (`n') ("`ctrl'")
}
}
postclose entry_unempatappx52
*/
/*
*2) BOOTSTRAP FOR SPENDING 
local sample 5152
use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
assert dib_sample == 1
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp
gen UR = unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
keep age52 UR age52xUR UR county_mofd tot_pmt fipscounty_firstnm fipscounty_firstnm_g years_since_covstart
save "$SSDIMed/data/analysis/bene-year_outcomes_sample-5152.dta", replace 
local clust county_mofd
local y tot_pmt 
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g

***verify same as current results
timer on 1
reghdfe `y' age52 UR age52xUR, abs(`controls_02') cluster(`clust')
timer off 1 
timer on 2
reghdfe `y' age52 UR age52xUR, abs(`controls_02') 
timer off 2
timer list 


cd "$SSDIMed/results/estimates/x-age52xUR"
postfile spending_unempatappX52 intercept age52 UR age52xUR sample str50 ctrl using "bootstrap_spending_cluster_county.dta", replace 
forval n=1/500{
di "`n'"
qui{
use "$SSDIMed/data/analysis/bene-year_outcomes_sample-5152.dta",  clear
rename (fipscounty_firstnm  ) (fipscounty_init`n' )
joinby fipscounty_init`n'  using "$SSDIMed/data/analysis/bootstrap/samples_cluster_county.dta"
gegen fipscounty_init=group(fipscounty_init`n')
local clust county_mofd
local y tot_pmt 
local controls_02 i.years_since_covstart##i.fipscounty_init
reghdfe `y' age52 UR age52xUR, abs(`controls_02') 
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post spending_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (`n') ("`ctrl'")
}
}
postclose spending_unempatappX52


*/
*2b) BOOTSTRAP FOR MORTALITY 
/*
local sample 5152
use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
assert dib_sample == 1
keep if inrange(age_year_covstart_fill, 51, 52)
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp
gen UR = unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR
local clust county_mofd
local y died_adj 
replace died_adj=died_adj*10^4 
keep `y' died_adj age52 UR age52xUR years_since_covstart fipscounty_firstnm_g fipscounty_firstnm male_init age county_mofd covstart_month
save "$SSDIMed/data/temp/bene-year_outcomes_sample-5152.dta", replace 
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g
local controls_04 i.years_since_covstart##i.fipscounty_firstnm_g i.male_init##i.age
local controls_10 i.years_since_covstart##i.fipscounty_firstnm_g i.male_init
***verify same as current results
reghdfe `y' age52 UR age52xUR, abs(`controls_02') cluster(`clust')


cd "$SSDIMed/results/estimates/x-age52xUR"
postfile mortality_unempatappX52 intercept age52 UR age52xUR sample str50 ctrl using "bootstrap_mortality_cluster_county.dta", replace 
forval n=5/100{
di "`n'"
qui{
use "$SSDIMed/data/temp/bene-year_outcomes_sample-5152.dta",  clear
rename (fipscounty_firstnm covstart_month ) (fipscounty_init`n' covstart_month`n')
joinby fipscounty_init`n' covstart_month`n' using "$SSDIMed/data/analysis/bootstrap/samples.dta"
gegen fipscounty_init=group(fipscounty_init`n')
local clust county_mofd
local y died_adj 
local controls_02 i.years_since_covstart##i.fipscounty_init
***verify same as current results
reghdfe `y' age52 UR age52xUR, abs(`controls_02') 
local intercept=_b[_cons]
local UR=_b[UR]
local age52=_b[age52]
local age52xUR=_b[age52xUR]
post mortality_unempatappX52 (`intercept') (`age52') (`UR') (`age52xUR') (`n') ("`ctrl'")
}
}
postclose mortality_unempatappX52
*/

*/

*3) bring together true and bootstrap results for alphas and betas  

clear 
local x age52xUR
local y incidence_pop_age_atapp
local clust county_mofd 
local sample 5152
foreach ctrl in "02"{
local results_stub $SSDIMed/results/estimates/x-age52xUR/x-age52xUR_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
use "`results_stub'.dta", clear 
if "`ctrl'"=="02" local ctrl "fipscounty_init"
replace var="intercept" if var=="_cons" & y=="`y'"
keep var coef controls 
reshape wide coef, i(controls) j(var) string 
rename controls ctrl 
rename coef* *
gen sample=0 
append using "$SSDIMed/results/estimates/x-age52xUR/bootstrap_incidence_cluster_county.dta",
*if "`ctrl'"=="none" keep if ctrl=="`ctrl'"
*if "`ctrl'"=="fipscounty_init" keep if regexm(ctrl, "fipscounty_init") & !regexm(ctrl, "covstart")
*if "`ctrl'"=="fipscounty_init covstart_month" keep if regexm(ctrl, "fipscounty_init") & regexm(ctrl, "covstart")
drop ctrl 
compress _all 
rename (intercept age52 UR age52xUR ) (alpha alpha52 alphaUR alpha52XUR)
save "$SSDIMed/results/estimates/modelparameterization/alphas_`ctrl'_cluster_county.dta", replace 
list in 1/10
sum 
}


clear 
local x age52xUR
local y tot_pmt 
local clust county_mofd 
local sample 5152
foreach ctrl in "02"{
local results_stub $SSDIMed/results/estimates/x-age52xUR/x-`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
use "`results_stub'.dta", clear 
*if "`ctrl'"=="02" local ctrl "fipscounty_init X years_since_covstart"
*if "`ctrl'"=="04" local ctrl "i.years_since_covstart##i.fipscounty_firstnm_g i.male_init##i.age"
replace var="intercept" if var=="_cons" & y=="`y'"
keep var coef absvars 
reshape wide coef, i(absvars) j(var) string 
rename absvars ctrl 
rename coef* *
gen sample=0 
append using "$SSDIMed/results/estimates/x-age52xUR/bootstrap_spending_cluster_county.dta",
*if "`ctrl'"== "countynum age_sex years_since_covstart" keep if ctrl=="fipscounty_init age_sex years_since_covs" | ctrl=="`ctrl'"
*if "`ctrl'"== "fipscounty_init X years_since_covstart" keep if regexm(ctrl, "fipscounty") & regexm(ctrl, "years_since_covstart")
*else keep if ctrl=="`ctrl'" 
drop ctrl 
compress _all 
rename (intercept age52 UR age52xUR ) (beta beta52 betaUR beta52XUR)
save "$SSDIMed/results/estimates/modelparameterization/betas_`ctrl'_cluster_county.dta", replace 
}


clear 
local x age52xUR
local y died_adj 
local clust county_mofd 
local sample 5152
foreach ctrl in "02"{
local results_stub $SSDIMed/results/estimates/x-age52xUR/x-`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
use "`results_stub'.dta", clear 
*if "`ctrl'"=="02" local ctrl "fipscounty_init X years_since_covstart"
*if "`ctrl'"=="04" local ctrl "i.years_since_covstart##i.fipscounty_firstnm_g i.male_init##i.age"
replace var="intercept" if var=="_cons" & y=="`y'"
keep var coef absvars 
reshape wide coef, i(absvars) j(var) string 
rename absvars ctrl 
rename coef* *
gen sample=0 
replace UR=UR*10^4
replace age52=age52*10^4
replace age52xUR=age52xUR*10^4
replace intercept=intercept*10^4
append using "$SSDIMed/results/estimates/x-age52xUR/bootstrap_mortality_cluster_county.dta",
*if "`ctrl'"== "countynum age_sex years_since_covstart" keep if ctrl=="fipscounty_init age_sex years_since_covs" | ctrl=="`ctrl'"
*if "`ctrl'"== "fipscounty_init X years_since_covstart" keep if regexm(ctrl, "fipscounty") & regexm(ctrl, "years_since_covstart")
*else keep if ctrl=="`ctrl'" 
drop ctrl 
compress _all 
rename (intercept age52 UR age52xUR ) (beta beta52 betaUR beta52XUR)

save "$SSDIMed/results/estimates/modelparameterization/betasmortality_`ctrl'_cluster_county.dta", replace 
}


*/

*4) do calculations 
****three column 
foreach column in "1" "2"{
*foreach column in "1"{
if "`column'"=="1"{
use "$SSDIMed/results/estimates/modelparameterization/alphas_fipscounty_init_cluster_county.dta", clear 
merge 1:1 sample using "$SSDIMed/results/estimates/modelparameterization/betas_02_cluster_county.dta", nogen   
}
if "`column'"=="2"{
use "$SSDIMed/results/estimates/modelparameterization/alphas_fipscounty_init_cluster_county.dta", clear 
merge 1:1 sample using "$SSDIMed/results/estimates/modelparameterization/betasmortality_02_cluster_county.dta", nogen  
}
*if "`column'"=="3"{
*use "$SSDIMed/results/estimates/modelparameterization/alphas_none.dta", clear 
*merge 1:1 sample using "$SSDIMed/results/estimates/modelparameterization/betas_years_since_covstart.dta", nogen   
*}
***benefit parameters
gen double m=2*beta52/alpha52 
gen double n=beta-(alpha*beta52/alpha52)
gen double mUR=2*(beta52+beta52XUR)/(alpha52+alpha52XUR) 
gen double nUR=beta+betaUR- (alpha+alphaUR)*(beta52+beta52XUR)/(alpha52+alpha52XUR) 
gen double mURminusm=2*(beta52+beta52XUR)/(alpha52+alpha52XUR) - 2*beta52/alpha52
gen double nURminusn=betaUR- (alpha+alphaUR)*(beta52+beta52XUR)/(alpha52+alpha52XUR) + (alpha*beta52/alpha52)
***elements of the figure
gen double x51=alpha 
gen double x51UR=alpha+alphaUR 
gen double x52=alpha+alpha52 
gen double x52UR=alpha+alpha52+alphaUR+alpha52XUR
gen double B_x51=n+m*x51 
gen double B_x51UR=nUR+mUR*x51UR 
gen double B_x52=n+m*x52
gen double B_x52UR=nUR+mUR*x52UR
***difference at x51
gen double BU_x51=nUR+mUR*x51
gen double diff_x51=BU_x51-B_x51
***set up CI 
gen double B_200=n+m*200
gen double BUR_200=nUR+mUR*200 
gen double B_800=n+m*800
gen double BUR_800=nUR+mUR*800 
***cost parameters
foreach DeltaC in -500 -50000 -5000{
local posDeltaC=-1*`DeltaC'
if "`DeltaC'"=="-500" local span51=2 
if "`DeltaC'"=="-500" local span52=40
if "`DeltaC'"=="-5000" local span51=.2 
if "`DeltaC'"=="-5000" local span52=4
if "`DeltaC'"=="-50000" local span51=.02 
if "`DeltaC'"=="-50000" local span52=.4
gen double mC_51`posDeltaC'=(-`DeltaC' -m*alpha -n +mUR *(alpha+alphaUR) +nUR)/alphaUR
gen double nC_51`posDeltaC'=m*alpha +n- mC_51`posDeltaC'*alpha
gen double alphaCF_51_C`posDeltaC' =(`DeltaC' +nC_51`posDeltaC'-n)/(m-mC_51`posDeltaC')
gen double mC_52`posDeltaC'=(-`DeltaC'  +mUR*(alpha+alphaUR+alpha52+alpha52XUR) -m*(alpha+alpha52) +nUR - n)/(alphaUR+alpha52XUR)
gen double nC_52`posDeltaC'=m*(alpha+alpha52)+n - mC_52`posDeltaC'*(alpha+alpha52)
gen double alphaCF_52_C`posDeltaC' =(`DeltaC' +nC_52`posDeltaC'-n)/(m-mC_52`posDeltaC')
gen double C_x51_lo`posDeltaC'=  nC_51`posDeltaC'         +mC_51`posDeltaC'*(x51-`span51')
gen double C_x51_hi`posDeltaC'=  nC_51`posDeltaC'         +mC_51`posDeltaC'*(x51+`span51')
gen double C_x51UR_lo`posDeltaC'=nC_51`posDeltaC'+`DeltaC'+mC_51`posDeltaC'*(x51UR -`span51')
gen double C_x51UR_hi`posDeltaC'=nC_51`posDeltaC'+`DeltaC'+mC_51`posDeltaC'*(x51UR +`span51')
gen double C_x52_lo`posDeltaC'=  nC_52`posDeltaC'         +mC_52`posDeltaC'*(x52-`span52')
gen double C_x52_hi`posDeltaC'=  nC_52`posDeltaC'         +mC_52`posDeltaC'*(x52+`span52')
gen double C_x52UR_lo`posDeltaC'=nC_52`posDeltaC'+`DeltaC'+mC_52`posDeltaC'*(x52UR-`span52')
gen double C_x52UR_hi`posDeltaC'=nC_52`posDeltaC'+`DeltaC'+mC_52`posDeltaC'*(x52UR+`span52')
}



*5) collapse and print table 
cap mkdir "$SSDIMed/results/temp"
cap mkdir "$SSDIMed/results/tables"
preserve 
keep in 1 
xpose, clear varname 
save "$SSDIMed/results/temp/tablecolumn`column'_value_cluster_county.dta", replace 
restore 
preserve 
drop in 1 
gcollapse (sd) *, fast 
xpose, clear varname 
save "$SSDIMed/results/temp/tablecolumn`column'_sd_cluster_county.dta", replace 
restore 
preserve 
drop in 1 
gcollapse (p5) *, fast 
xpose, clear varname 
save "$SSDIMed/results/temp/tablecolumn`column'_lb_cluster_county.dta", replace 
restore 
preserve 
drop in 1 
gcollapse (p95) *, fast 
xpose, clear varname 
save "$SSDIMed/results/temp/tablecolumn`column'_ub_cluster_county.dta", replace
restore 
clear 
gen stat=""
foreach var in value sd lb ub{
append using "$SSDIMed/results/temp/tablecolumn`column'_`var'_cluster_county.dta"
replace stat="`var'" if stat==""
}
reshape wide v1, i(_varname) j(stat) string 
rename v1* * 
rename _varname var 
drop if var=="sample"
rename (lb sd ub value) (lb`column' sd`column' ub`column' value`column')
save "$SSDIMed/results/estimates/modelparameterization/tablecolumn`column'_cluster_county.dta", replace 
}




****final table output 
use "$SSDIMed/results/estimates/modelparameterization/tablecolumn1_cluster_county.dta", clear 
merge 1:1 var using "$SSDIMed/results/estimates/modelparameterization/tablecolumn2_cluster_county.dta", nogen 
***drop figure elements 
gen dropme=inlist(var, "x51", "x52",  "", "B_x51UR", "B_x52", "B_x52UR")
replace dropme=1 if inlist(var, "alpha", "alpha52", "alpha52XUR", "alphaUR", "B_200", "BUR_200")
replace dropme=1 if inlist(var, "beta", "beta52", "beta52XUR", "betaUR", "B_800", "BUR_800")
***keep only deltaC=-5000, except for Panel C stuff 
local posDeltaC 5000
replace dropme=1 if regexm(var, "500")
replace dropme=0 if regexm(var, "5000")
replace dropme=1 if regexm(var, "50000")
replace dropme=0 if regexm(var, "alphaCF")
replace dropme=1 if regexm(var, "hi") | regexm(var, "lo")
drop if dropme==1
drop ub* lb* dropme 
*reshape wide value sd, i(var) j(column) string 
gen counter=1 if var=="m"
replace counter=2 if var=="n"
replace counter=3 if var=="mUR"
replace counter=4 if var=="nUR"
replace counter=5 if var=="mURminusm"
replace counter=6 if var=="nURminusn"
replace counter=6.25 if var=="BU_x51"
replace counter=6.5 if var=="B_x51"
replace counter=6.75 if var=="diff_x51"
replace counter=7 if var=="mC_51`posDeltaC'"
replace counter=8 if var=="nC_51`posDeltaC'"
replace counter=9 if var=="mC_52`posDeltaC'"
replace counter=10 if var=="nC_52`posDeltaC'"
replace counter=11 if var=="x51UR"
replace counter=12 if var=="alphaCF_51_C500"
replace counter=13 if var=="alphaCF_51_C5000"
replace counter=14 if var=="alphaCF_51_C50000"
replace counter=15 if var=="x52UR"
replace counter=16 if var=="alphaCF_52_C500"
replace counter=17 if var=="alphaCF_52_C5000"
replace counter=18 if var=="alphaCF_52_C50000"
sort counter 
drop counter 
order var value1 sd1 value2 sd2 
export excel $SSDIMed/results/tables/modelparameterization_cluster_county.xls, firstrow(variables) replace 



